<template>
    <div>
        <navBar :navTitle="$t('Genggaimima')" />
        <div class="full-width92">
            <van-form ref="refFormPassword" class="public-form">
                <div>
                    <div class="inp-label">{{ $t('Dangqianmima') }}</div>
                    <van-field v-model.trim="formPassword.old_password" class="bg-inp"
                        :placeholder="$t('Qingshuru') + $t('Dangqianmima')" clearable type="password"
                        :rules="[{ required: true, message: $t('Qingshuru') + $t('Dangqianmima') }]" />
                </div>
                <div>
                    <div class="inp-label">{{ $t('Xinmima') }}</div>
                    <van-field v-model.trim="formPassword.password" class="bg-inp"
                        :placeholder="$t('Qingshuru') + $t('Xinmima')" clearable type="password"
                        :rules="[{ required: true, message: $t('Qingshuru') + $t('Xinmima') }]" />
                </div>

                <div>
                    <div class="inp-label">{{ $t('Querenxinmima') }}</div>
                    <van-field v-model.trim="formPassword.check_password" class="bg-inp"
                        :placeholder="$t('Qingshuru') + $t('Querenxinmima')" clearable type="password" :rules="[{ required: true, message: $t('Qingshuru') + $t('Querenxinmima') },
                        { validator: checkPass, message: $t('Qingshuru') + $t('Querenxinmima') }]" />
                </div>
            </van-form>

            <van-button block type="info" native-type="submit" class="marTop40" @click="fnBtnSave"
                v-btn-re-click>{{ $t('Chongzhi') }}
            </van-button>
        </div>
    </div>
</template>

<script>
import { quser_edit_api } from "@/assets/js/api";

export default {
    name: "myPassword",
    data() {
        return {
            formPassword: {
                old_password: "",
                password: "",
                check_password: "",
            },
        }
    },
    methods: {
        /*確認密碼 验证*/
        checkPass(newPass) {
            console.log(newPass);
            return newPass === this.formPassword.password;
            // return /^1\d{10}$/.test(val) // 返回true或false
        },

        fnBtnSave() {
            this.$refs.refFormPassword.validate().then(() => {
                quser_edit_api({
                    id: localStorage.getItem("userId"),
                    password: this.formPassword.password,
                    old_password: this.formPassword.old_password,
                }).then(res => {
                    if (res.code == 200) {
                        this.$toast(res.msg || "Đặt lại mật khẩu thành công！");
                        localStorage.removeItem("userId");
                        setTimeout(() => {
                            this.$router.push({
                                path: "/login",
                            })
                        }, 1000)
                    } else {
                        this.$toast(this.$t('Dngaqianmimacuo'));
                    }
                })
            }).catch(res => { })
        }
    },
    created() {

    },
}
</script>
